package com.example.cmpe277stockdata.RSSFeeds;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.example.cmpe277stockdata.beans.CompanyInformationBean;
import com.example.cmpe277stockdata.beans.HistoricalBean;
import com.example.cmpe277stockdata.constants.DataConstants;

public class CompanyInformationReader 
{
	public CompanyInformationBean getCompanyInformation(String companySymbol)
	{
		String URLStringCompanyInfoBasic = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.stocks%20where%20symbol%3D'"+companySymbol+"'&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
		String URLStringCompanyInfoDetailed = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D'"+companySymbol+"'&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
		CompanyInformationBean company = new CompanyInformationBean();
		try {
			
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			URL u,u1;
		
			u = new URL(URLStringCompanyInfoBasic);
			u1 = new URL(URLStringCompanyInfoDetailed);
			
			Document doc = builder.parse(u.openStream());
			NodeList nodes = doc.getElementsByTagName(DataConstants.COMPANY_INFORMATION_TAG);
			
			System.out.println(nodes.getLength());
			for(int i=0;i<nodes.getLength();i++) 
			{
				Element element = (Element)nodes.item(i);	
				company.setSector(getElementValue(element,DataConstants.SECTOR));
				company.setIndustry(getElementValue(element,DataConstants.INDUSTRY));
				company.setFullTimeEmployees(getElementValue(element,DataConstants.FULL_TIME_EMPLOYEES));
			}
			
			doc = builder.parse(u1.openStream());
			nodes = doc.getElementsByTagName(DataConstants.HISTORICAL_MAIN);
			
			for(int i=0;i<nodes.getLength();i++) 
			{
				Element element = (Element)nodes.item(i);	
				company.setAverageDailyVolume(getElementValue(element,DataConstants.DAILY_VOLUME));
				company.setDaysHigh(getElementValue(element,DataConstants.DAYS_HIGH));
				company.setDaysLow(getElementValue(element,DataConstants.DAYS_LOW));
				company.setPercentChange(getElementValue(element,DataConstants.PERCENT_CHANGE));
				company.setDividendShare(getElementValue(element,DataConstants.DIVIDENT_SHARE));
				company.setYearsHigh(getElementValue(element,DataConstants.YEARS_HIGH));
				company.setYearsLow(getElementValue(element,DataConstants.YEARS_LOW));
				company.setMarketCapitalization(getElementValue(element,DataConstants.MARKET_CAPITALIZATION));
				company.setPERatio(getElementValue(element,DataConstants.PE_RATIO));
				company.setCompanyFullName(getElementValue(element,DataConstants.COMPANY_FULLNAME));
			}
			
			return company;
		} 
		catch (MalformedURLException e) 
		{
			System.err.println(e.getMessage());
		} 
		catch (SAXException e) 
		{
			System.err.println(e.getMessage());
		}
		catch (IOException e) 
		{
			System.err.println(e.getMessage());
		} 
		catch (ParserConfigurationException e)
		{
			System.err.println(e.getMessage());
		}
		return company;
	}

	protected static String getElementValue(Element parent, String label)
	{
		return getCharacterDataFromElement((Element) parent
				.getElementsByTagName(label).item(0));
	}

	private static String getCharacterDataFromElement(Element e) 
	{
		try 
		{
			Node child = e.getFirstChild();
			if (child instanceof CharacterData) {
				CharacterData cd = (CharacterData) child;
				return cd.getData();
			}
		} 
		catch (Exception ex) {
			System.err.println(ex.getMessage());
		}
		return "";
	}
}
